package com.ruoyi.law.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.law.domain.ContentField;
import com.ruoyi.law.service.IContentFieldService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自定义字段Controller
 * 
 * @author hwx
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/contentField/field")
public class ContentFieldController extends BaseController
{
    @Autowired
    private IContentFieldService contentFieldService;

    /**
     * 查询自定义字段列表
     */
    @PreAuthorize("@ss.hasPermi('contentField:field:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContentField contentField)
    {
        startPage();
        List<ContentField> list = contentFieldService.selectContentFieldList(contentField);
        return getDataTable(list);
    }

    /**
     * 导出自定义字段列表
     */
    @PreAuthorize("@ss.hasPermi('contentField:field:export')")
    @Log(title = "自定义字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContentField contentField)
    {
        List<ContentField> list = contentFieldService.selectContentFieldList(contentField);
        ExcelUtil<ContentField> util = new ExcelUtil<ContentField>(ContentField.class);
        util.exportExcel(response, list, "自定义字段数据");
    }

    /**
     * 获取自定义字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('contentField:field:query')")
    @GetMapping(value = "/{fieldId}")
    public AjaxResult getInfo(@PathVariable("fieldId") Long fieldId)
    {
        return success(contentFieldService.selectContentFieldByFieldId(fieldId));
    }

    /**
     * 新增自定义字段
     */
    @PreAuthorize("@ss.hasPermi('contentField:field:add')")
    @Log(title = "自定义字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContentField contentField)
    {
        return toAjax(contentFieldService.insertContentField(contentField));
    }

    /**
     * 修改自定义字段
     */
    @PreAuthorize("@ss.hasPermi('contentField:field:edit')")
    @Log(title = "自定义字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContentField contentField)
    {
        return toAjax(contentFieldService.updateContentField(contentField));
    }

    /**
     * 删除自定义字段
     */
    @PreAuthorize("@ss.hasPermi('contentField:field:remove')")
    @Log(title = "自定义字段", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fieldIds}")
    public AjaxResult remove(@PathVariable Long[] fieldIds)
    {
        return toAjax(contentFieldService.deleteContentFieldByFieldIds(fieldIds));
    }
}
